﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Odbc;
using System.Data;
using System.IO;
using System.Drawing;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Windows;

/// <summary>
/// Summary description for FeedbackResult
/// </summary>
public class FeedbackResult
{
    const int limit_Course_Close = 5; //MAX=7
    const int limit_Inst_Close = 4; //MAX=6
    const int limit_tut_lab_Close = 1; //MAX=2
    const int limit_resp_TA_Open = 1; //MAX=1
    const int limit_resp_Open = 3; //MAX=5

    DateTime curr_date;
    String dt_sql;
    String physicalApplicationPath;
    string resultTxtPath;
    string resultPDFPath;
    string feedbackID;
    string courseID;

    public FeedbackResult(string phyapppath)
    {
        physicalApplicationPath = phyapppath;
        //
        // TODO: Add constructor logic here
        //
    }

    public DataSet getAllResponse(string courseID, string feedbackID)
    {
        this.feedbackID = feedbackID;
        this.courseID = courseID;
        curr_date = DateTime.Today;
        dt_sql = curr_date.ToString("yyyy-MM-dd");

        string sql = "Select * FROM response WHERE Feedback_ID=" + feedbackID;
        DbUtilities responses = new DbUtilities(sql);
        DataSet dataset = new DataSet();
        dataset = responses.retrieveInfo();

        return dataset;
    }

    public void revealResult()
    {
        Mailer mailer = new Mailer();
        mailer.mailResult(resultTxtPath, resultPDFPath);

        //DELETING FEEDBACK
        string sql1 = "Delete FROM response WHERE Feedback_ID=" + feedbackID;
        DbUtilities deleteResponse = new DbUtilities(sql1);
        deleteResponse.writeInfo();

        string sql2 = "Delete FROM instructor WHERE Feedback_ID=" + feedbackID;
        DbUtilities deleteInstructor = new DbUtilities(sql2);
        deleteInstructor.writeInfo();

        string sql3 = "Delete FROM co_instructor WHERE Feedback_ID=" + feedbackID;
        DbUtilities deleteCoInstructor = new DbUtilities(sql3);
        deleteCoInstructor.writeInfo();

        string sql4 = "Delete FROM ta WHERE Feedback_ID=" + feedbackID;
        DbUtilities deleteTA = new DbUtilities(sql4);
        deleteTA.writeInfo();

        string sql5 = "Delete FROM student WHERE Feedback_ID=" + feedbackID;
        DbUtilities deleteStudent = new DbUtilities(sql5);
        deleteStudent.writeInfo();

        string sql6 = "Delete FROM course_feedback WHERE Feedback_ID=" + feedbackID;
        DbUtilities deleteFeedback = new DbUtilities(sql6);
        deleteFeedback.writeInfo();

    }

    public void calculateResult(DataSet dataSet,string courseID,string feedbackID)
    {
        const int limit_Course_Close = 5; //MAX=7
        const int limit_Inst_Close = 4; //MAX=6
        const int limit_tut_lab_Close = 1; //MAX=2
        const int limit_resp_TA_Open = 1; //MAX=1
        const int limit_resp_Open = 3; //MAX=5

        int totalResp = dataSet.Tables[0].Rows.Count;

        int[,] resp_Course_Close = new int[totalResp, limit_Course_Close];
        int[,] resp_Inst_Close = new int[totalResp, limit_Inst_Close];
        int[,] resp_tut_lab_Close = new int[totalResp, limit_tut_lab_Close];
        string[,] resp_TA_Open = new string[totalResp, limit_resp_TA_Open];
        string[,] resp_Open = new string[totalResp, limit_resp_Open];

        int[,] pie_Course_Close = new int[limit_Course_Close, 6];
        int[,] pie_Inst_Close = new int[limit_Inst_Close, 6];
        int[,] pie_tut_lab_Close = new int[limit_tut_lab_Close, 6];

        for (int i = 0; i < totalResp; i++)
        {
            for (int j = 0; j < limit_Course_Close; j++)
            {
                resp_Course_Close[i, j] = (int)dataSet.Tables[0].Rows[i].ItemArray[1 + j];
                pie_Course_Close[j, resp_Course_Close[i, j]] += 1;
            }

            for (int j = 0; j < limit_Inst_Close; j++)
            {
                resp_Inst_Close[i, j] = (int)dataSet.Tables[0].Rows[i].ItemArray[8 + j];
                pie_Inst_Close[j, resp_Inst_Close[i, j]] += 1;
            }

            for (int j = 0; j < limit_tut_lab_Close; j++)
            {
                resp_tut_lab_Close[i, j] = (int)dataSet.Tables[0].Rows[i].ItemArray[14 + j];
                pie_tut_lab_Close[j, resp_tut_lab_Close[i, j]] += 1;
            }
        }

        //___________WRITING ALL RESPONSES _____________________________________________________________________

        String fileName;
        fileName = feedbackID + "_" + courseID + "_" + dt_sql + ".txt";
        //this.Request.PhysicalApplicationPath
        resultTxtPath=physicalApplicationPath + "\\Summarized Feedbacks\\" + fileName;
        FileStream responseFile = new FileStream(physicalApplicationPath + "\\Summarized Feedbacks\\" + fileName, FileMode.Create);
        StreamWriter responseWriter = new StreamWriter(responseFile);


        responseWriter.WriteLine("COURSE ID : " + courseID);
        responseWriter.WriteLine("FEEDBACK ID : " + feedbackID + "\n");



        for (int i = 0; i < totalResp; i++)
        {
            responseWriter.WriteLine();
            responseWriter.WriteLine("\n------------------------------------------------------------------------------------\n");
            responseWriter.WriteLine("Response " + (i + 1).ToString() + " of " + totalResp + "\n");

            //COURSE
            responseWriter.WriteLine();
            responseWriter.WriteLine("\nCOURSE\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. Objectives and plan of the course were: 5(Very clear) - 1(Not at all)\n");
            responseWriter.WriteLine("A. " + ((int)dataSet.Tables[0].Rows[i].ItemArray[1]).ToString() + "\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. Coverage and depth of material was: 5(Excellent) - 1(Very poor)\n");
            responseWriter.WriteLine("A. " + ((int)dataSet.Tables[0].Rows[i].ItemArray[2]).ToString() + "\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. How useful was the textbook/reference material: 5(Very useful) - 1(Not at all)\n");
            responseWriter.WriteLine("A. " + ((int)dataSet.Tables[0].Rows[i].ItemArray[3]).ToString() + "\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. Overall difficulty level of the course: 5(Very difficult) - 1(Very easy)\n");
            responseWriter.WriteLine("A. " + ((int)dataSet.Tables[0].Rows[i].ItemArray[4]).ToString() + "\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. How much you learnt in the course: 5(A lot) - 1(Very little)\n");
            responseWriter.WriteLine("A. " + ((int)dataSet.Tables[0].Rows[i].ItemArray[5]).ToString() + "\n");

            //INSTRUCTION
            responseWriter.WriteLine();
            responseWriter.WriteLine("\nINSTRUCTION\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. Clarity and organization of lectures were: 5(Excellent) - 1(Not at all)\n");
            responseWriter.WriteLine("A. " + ((int)dataSet.Tables[0].Rows[i].ItemArray[8]).ToString() + "\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. Sufficient and suitable assignments/projects/labs were given: 5(Too many) - 1(Too few)\n");
            responseWriter.WriteLine("A. " + ((int)dataSet.Tables[0].Rows[i].ItemArray[9]).ToString() + "\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. Sufficient and suitable tests/exams/quizzes were given to check your understanding: 5(Too many) - 1(Too few)\n");
            responseWriter.WriteLine("A. " + ((int)dataSet.Tables[0].Rows[i].ItemArray[10]).ToString() + "\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. The overall quality of teaching in this course was: 5(Outstanding) - 1(Poor)\n");
            responseWriter.WriteLine("A. " + ((int)dataSet.Tables[0].Rows[i].ItemArray[11]).ToString() + "\n");

            //TUTORIAL/LAB
            responseWriter.WriteLine();
            responseWriter.WriteLine("\nTUTORIAL/LAB\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. How helpful was the tutorial/lab: 5(Very helpful) - 1(Not at all)\n");
            responseWriter.WriteLine("A. " + ((int)dataSet.Tables[0].Rows[i].ItemArray[14]).ToString() + "\n");

            //TA
            responseWriter.WriteLine();
            responseWriter.WriteLine("\nTA\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. How effective was his/her role in the course:\n");
            responseWriter.WriteLine("A. " + dataSet.Tables[0].Rows[i].ItemArray[16].ToString() + "\n");

            //OTHER OPEN
            responseWriter.WriteLine();
            responseWriter.WriteLine("\nOTHER OPEN\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. What did you like about the course:\n");
            responseWriter.WriteLine("A. " + dataSet.Tables[0].Rows[i].ItemArray[17].ToString() + "\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. What did you not like about the course:\n");
            responseWriter.WriteLine("A. " + dataSet.Tables[0].Rows[i].ItemArray[18].ToString() + "\n");

            responseWriter.WriteLine();
            responseWriter.WriteLine("\nQ. Any other comment/suggestion:\n");
            responseWriter.WriteLine("A. " + dataSet.Tables[0].Rows[i].ItemArray[19].ToString() + "\n");
        }

        responseWriter.Close();
        responseFile.Close();



        //Initialize PDF
        string filePathPDF;
        string fileNamePDF;
        Document doc;
        fileNamePDF = "SUM_" + feedbackID + "_" + courseID + "_" + dt_sql + ".pdf";
        filePathPDF = physicalApplicationPath + "\\Summarized Feedbacks\\" + fileNamePDF;
        resultPDFPath=filePathPDF;
        doc = new Document();
        PdfWriter.GetInstance(doc, new FileStream(filePathPDF, FileMode.Create));
        doc.Open();

        //doc.Add(new Paragraph("SUMMARY OF FEEDBACK",FontFactory.GetFont(FontFactory.HELVETICA_BOLD,14));
        Paragraph heading1 = new Paragraph("\nSUMMARY OF FEEDBACK", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 20));
        heading1.Alignment = Element.ALIGN_CENTER;
        doc.Add(heading1);
        doc.Add(new Paragraph("\n\n"));

        Paragraph heading_courseID = new Paragraph("COURSE ID : " + courseID, FontFactory.GetFont(FontFactory.TIMES_BOLD, 14));
        heading_courseID.Alignment = Element.ALIGN_CENTER;
        doc.Add(heading_courseID);

        Paragraph heading_feedbackID = new Paragraph("FEEDBACK ID : " + feedbackID, FontFactory.GetFont(FontFactory.TIMES_BOLD, 14));
        heading_feedbackID.Alignment = Element.ALIGN_CENTER;
        doc.Add(heading_feedbackID);



        //__________CREATING PIE CHARTS________________________________________________________

        //BRUSHES
        Brush brushRed = new SolidBrush(Color.Red);
        Brush brushBlue = new SolidBrush(Color.Blue);
        Brush brushGreen = new SolidBrush(Color.Green);
        Brush brushYellow = new SolidBrush(Color.Yellow);
        Brush brushViolet = new SolidBrush(Color.Violet);
        Brush brushBlack = new SolidBrush(Color.Black);

        //FONTS
        System.Drawing.Font fontLegend = new System.Drawing.Font("Verdana", 10);
        System.Drawing.Font fontTitle = new System.Drawing.Font("Verdana", 15, FontStyle.Bold);

        //TITLE
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;

        int bufferSpace = 15;
        int titleHeight = fontTitle.Height + bufferSpace;

        //System.Drawing.Image testPie = System.Drawing.Image.FromFile("C:\\Users\\Ankit\\Pictures\\pie.jpg");
        //System.Drawing.Bitmap testbmp= new Bitmap(460,760);
        //System.Drawing.Image testPie = System.Drawing.Image.FromFile(this.Request.PhysicalApplicationPath + "\\App_Images\\pie.jpg");
        //System.Drawing.Graphics graphicsPie = System.Drawing.Graphics.FromImage(testPie);

        //graphicsPie.DrawString("Title", fontTitle, new SolidBrush(Color.Black), new System.Drawing.Rectangle(0, 0, 460, titleHeight), stringFormat);
        Pen penPie = new Pen(Color.Black, 3);
        int pieHeight = 400;
        System.Drawing.Rectangle rectanglePie = new System.Drawing.Rectangle(30, titleHeight + 20, pieHeight, pieHeight);



        if (totalResp > 0) //Condition to check if no. of responses is > 0
        {

            //PIE CHARTS for COURSE_CLOSED

            for (int i = 0; i < limit_Course_Close; i++)
            {
                doc.NewPage();

                Paragraph section_header = new Paragraph("COURSE\nQuestion : " + (i + 1).ToString() + "\n\n", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
                section_header.Alignment = Element.ALIGN_CENTER;
                doc.Add(section_header);

                string[] ques_course_close = new string[limit_Course_Close];
                ques_course_close[0] = "Objectives and plan of the course were: \n5(Very clear) - 1(Not at all)";
                ques_course_close[1] = "Coverage and depth of material was: \n5(Excellent) - 1(Very poor)";
                ques_course_close[2] = "How useful was the textbook/reference material: \n5(Very useful) - 1(Not at all)";
                ques_course_close[3] = "Overall difficulty level of the course: \n5(Very difficult) - 1(Very easy)";
                ques_course_close[4] = "How much you learnt in the course: \n5(A lot) - 1(Very little)";

                Paragraph ques_string = new Paragraph(ques_course_close[i], FontFactory.GetFont(FontFactory.TIMES_BOLD, 12));
                ques_string.Alignment = Element.ALIGN_CENTER;

                doc.Add(new Paragraph(ques_string));
                //doc.Add(new Paragraph("\n"));

                System.Drawing.Image testPie = System.Drawing.Image.FromFile(physicalApplicationPath + "\\App_Images\\pie.jpg");
                System.Drawing.Graphics graphicsPie = System.Drawing.Graphics.FromImage(testPie);
                //graphicsPie.DrawString("Title", fontTitle, new SolidBrush(Color.Black), new System.Drawing.Rectangle(0, 0, 460, titleHeight), stringFormat);

                int[] startPie = new int[6];
                int[] anglePie = new int[6];
                double[] angleActual = new double[6];
                double[] percActual = new double[6];

                startPie[0] = 0;
                anglePie[0] = 0;
                angleActual[0] = 0;
                percActual[0] = 0;

                for (int j = 1; j < 6; j++)
                {
                    startPie[j] = startPie[j - 1] + anglePie[j - 1];
                    angleActual[j] = (((double)pie_Course_Close[i, j]) * 360) / totalResp;
                    if ((angleActual[j] < 1) && (angleActual[j] > 0))
                        anglePie[j] = 1;
                    else
                        anglePie[j] = (pie_Course_Close[i, j] * 360) / totalResp;
                    percActual[j] = (pie_Course_Close[i, j] / totalResp) * 100;

                }

                anglePie[5] = 360 - startPie[5];

                graphicsPie.FillPie(brushRed, rectanglePie, startPie[1], anglePie[1]);
                graphicsPie.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                graphicsPie.FillPie(brushBlue, rectanglePie, startPie[2], anglePie[2]);
                graphicsPie.FillPie(brushGreen, rectanglePie, startPie[3], anglePie[3]);
                graphicsPie.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                graphicsPie.FillPie(brushYellow, rectanglePie, startPie[4], anglePie[4]);
                graphicsPie.FillPie(brushViolet, rectanglePie, startPie[5], anglePie[5]);

                string imgPath = physicalApplicationPath + "\\App_Images\\Temp\\" + "pieChart_CourseClose" + i.ToString() + ".png";
                graphicsPie.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                testPie.Save(imgPath, System.Drawing.Imaging.ImageFormat.Png);
                testPie.Dispose();
                graphicsPie.Dispose();
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imgPath);
                img.ScalePercent(60);
                img.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
                doc.Add(img);

                //Showing keys
                PdfPTable keyTable = new PdfPTable(3);
                PdfPCell[] cellRow1 = new PdfPCell[3];
                PdfPCell[] cellRow2 = new PdfPCell[3];
                PdfPCell[] cellRow3 = new PdfPCell[3];
                PdfPCell[] cellRow4 = new PdfPCell[3];
                PdfPCell[] cellRow5 = new PdfPCell[3];


                PdfPRow[] row_complete = new PdfPRow[5];

                Color[] keyColor = new Color[5];
                keyColor[0] = Color.Red;
                keyColor[1] = Color.Blue;
                keyColor[2] = Color.Green;
                keyColor[3] = Color.Yellow;
                keyColor[4] = Color.Violet;


                for (int keyi = 0; keyi < 5; keyi++)
                {
                    /*string keyImgPath =this.Request.PhysicalApplicationPath + "\\App_Images\\" + (keyi + 1).ToString() + ".png";

                    iTextSharp.text.Image keyImg = iTextSharp.text.Image.GetInstance(keyImgPath);
                    keyImg.ScalePercent(70);
                    keyImg.Alignment = iTextSharp.text.Image.TEXTWRAP | iTextSharp.text.Image.ALIGN_LEFT;*/
                    if (keyi == 0)
                    {
                        cellRow1[0] = new PdfPCell();
                        cellRow1[1] = new PdfPCell();
                        cellRow1[2] = new PdfPCell();

                        cellRow1[0].AddElement(new Paragraph("\n"));
                        cellRow1[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow1[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow1[2].AddElement(new Paragraph(pie_Course_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow1);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 1)
                    {
                        cellRow2[0] = new PdfPCell();
                        cellRow2[1] = new PdfPCell();
                        cellRow2[2] = new PdfPCell();

                        cellRow2[0].AddElement(new Paragraph("\n"));
                        cellRow2[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow2[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow2[2].AddElement(new Paragraph(pie_Course_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow2);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 2)
                    {
                        cellRow3[0] = new PdfPCell();
                        cellRow3[1] = new PdfPCell();
                        cellRow3[2] = new PdfPCell();

                        cellRow3[0].AddElement(new Paragraph("\n"));
                        cellRow3[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow3[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow3[2].AddElement(new Paragraph(pie_Course_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow3);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 3)
                    {
                        cellRow4[0] = new PdfPCell();
                        cellRow4[1] = new PdfPCell();
                        cellRow4[2] = new PdfPCell();

                        cellRow4[0].AddElement(new Paragraph("\n"));
                        cellRow4[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow4[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow4[2].AddElement(new Paragraph(pie_Course_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow4);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 4)
                    {
                        cellRow5[0] = new PdfPCell();
                        cellRow5[1] = new PdfPCell();
                        cellRow5[2] = new PdfPCell();

                        cellRow5[0].AddElement(new Paragraph("\n"));
                        cellRow5[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow5[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow5[2].AddElement(new Paragraph(pie_Course_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow5);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                }

                doc.Add(keyTable);
            }



            //PIE CHARTS for INSTRUCTION_CLOSED

            for (int i = 0; i < limit_Inst_Close; i++)
            {
                doc.NewPage();

                Paragraph section_header = new Paragraph("INSTRUCTION\nQuestion : " + (i + 1).ToString() + "\n\n", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
                section_header.Alignment = Element.ALIGN_CENTER;
                doc.Add(section_header);

                string[] ques_inst_close = new string[limit_Inst_Close];
                ques_inst_close[0] = "Clarity and organization of lectures were: \n5(Excellent) - 1(Not at all)";
                ques_inst_close[1] = "Sufficient and suitable assignments/projects/labs were given: \n5(Too many) - 1(Too few)";
                ques_inst_close[2] = "Sufficient and suitable tests/exams/quizzes were given to check your understanding: \n5(Too many) - 1(Too few)";
                ques_inst_close[3] = "The overall quality of teaching in this course was: \n5(Outstanding) - 1(Poor)";

                Paragraph ques_string = new Paragraph(ques_inst_close[i], FontFactory.GetFont(FontFactory.TIMES_BOLD, 12));
                ques_string.Alignment = Element.ALIGN_CENTER;

                doc.Add(new Paragraph(ques_string));
                //doc.Add(new Paragraph("\n"));

                System.Drawing.Image testPie = System.Drawing.Image.FromFile(physicalApplicationPath + "\\App_Images\\pie.jpg");
                System.Drawing.Graphics graphicsPie = System.Drawing.Graphics.FromImage(testPie);
                //graphicsPie.DrawString("Title", fontTitle, new SolidBrush(Color.Black), new System.Drawing.Rectangle(0, 0, 460, titleHeight), stringFormat);

                int[] startPie = new int[6];
                int[] anglePie = new int[6];
                double[] angleActual = new double[6];
                double[] percActual = new double[6];

                startPie[0] = 0;
                anglePie[0] = 0;
                angleActual[0] = 0;
                percActual[0] = 0;

                for (int j = 1; j < 6; j++)
                {
                    startPie[j] = startPie[j - 1] + anglePie[j - 1];
                    angleActual[j] = (((double)pie_Inst_Close[i, j]) * 360) / totalResp;
                    if ((angleActual[j] < 1) && (angleActual[j] > 0))
                        anglePie[j] = 1;
                    else
                        anglePie[j] = (pie_Inst_Close[i, j] * 360) / totalResp;
                    percActual[j] = (pie_Inst_Close[i, j] / totalResp) * 100;

                }

                anglePie[5] = 360 - startPie[5];

                graphicsPie.FillPie(brushRed, rectanglePie, startPie[1], anglePie[1]);
                graphicsPie.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                graphicsPie.FillPie(brushBlue, rectanglePie, startPie[2], anglePie[2]);
                graphicsPie.FillPie(brushGreen, rectanglePie, startPie[3], anglePie[3]);
                graphicsPie.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                graphicsPie.FillPie(brushYellow, rectanglePie, startPie[4], anglePie[4]);
                graphicsPie.FillPie(brushViolet, rectanglePie, startPie[5], anglePie[5]);

                string imgPath = physicalApplicationPath + "\\App_Images\\Temp\\" + "pieChart_InstClose" + i.ToString() + ".png";
                graphicsPie.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                testPie.Save(imgPath, System.Drawing.Imaging.ImageFormat.Png);
                testPie.Dispose();
                graphicsPie.Dispose();
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imgPath);
                img.ScalePercent(60);
                img.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
                doc.Add(img);

                //Showing keys
                PdfPTable keyTable = new PdfPTable(3);
                PdfPCell[] cellRow1 = new PdfPCell[3];
                PdfPCell[] cellRow2 = new PdfPCell[3];
                PdfPCell[] cellRow3 = new PdfPCell[3];
                PdfPCell[] cellRow4 = new PdfPCell[3];
                PdfPCell[] cellRow5 = new PdfPCell[3];


                PdfPRow[] row_complete = new PdfPRow[5];

                Color[] keyColor = new Color[5];
                keyColor[0] = Color.Red;
                keyColor[1] = Color.Blue;
                keyColor[2] = Color.Green;
                keyColor[3] = Color.Yellow;
                keyColor[4] = Color.Violet;

                for (int keyi = 0; keyi < 5; keyi++)
                {
                    /*string keyImgPath =this.Request.PhysicalApplicationPath + "\\App_Images\\" + (keyi + 1).ToString() + ".png";

                    iTextSharp.text.Image keyImg = iTextSharp.text.Image.GetInstance(keyImgPath);
                    keyImg.ScalePercent(70);
                    keyImg.Alignment = iTextSharp.text.Image.TEXTWRAP | iTextSharp.text.Image.ALIGN_LEFT;*/
                    if (keyi == 0)
                    {
                        cellRow1[0] = new PdfPCell();
                        cellRow1[1] = new PdfPCell();
                        cellRow1[2] = new PdfPCell();

                        cellRow1[0].AddElement(new Paragraph("\n"));
                        cellRow1[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow1[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow1[2].AddElement(new Paragraph(pie_Inst_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow1);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 1)
                    {
                        cellRow2[0] = new PdfPCell();
                        cellRow2[1] = new PdfPCell();
                        cellRow2[2] = new PdfPCell();

                        cellRow2[0].AddElement(new Paragraph("\n"));
                        cellRow2[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow2[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow2[2].AddElement(new Paragraph(pie_Inst_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow2);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 2)
                    {
                        cellRow3[0] = new PdfPCell();
                        cellRow3[1] = new PdfPCell();
                        cellRow3[2] = new PdfPCell();

                        cellRow3[0].AddElement(new Paragraph("\n"));
                        cellRow3[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow3[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow3[2].AddElement(new Paragraph(pie_Inst_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow3);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 3)
                    {
                        cellRow4[0] = new PdfPCell();
                        cellRow4[1] = new PdfPCell();
                        cellRow4[2] = new PdfPCell();

                        cellRow4[0].AddElement(new Paragraph("\n"));
                        cellRow4[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow4[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow4[2].AddElement(new Paragraph(pie_Inst_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow4);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 4)
                    {
                        cellRow5[0] = new PdfPCell();
                        cellRow5[1] = new PdfPCell();
                        cellRow5[2] = new PdfPCell();

                        cellRow5[0].AddElement(new Paragraph("\n"));
                        cellRow5[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow5[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow5[2].AddElement(new Paragraph(pie_Inst_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow5);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                }

                doc.Add(keyTable);

            }

            //PIE CHARTS for TUTORIAL_LAB_CLOSED

            for (int i = 0; i < limit_tut_lab_Close; i++)
            {
                doc.NewPage();

                Paragraph section_header = new Paragraph("TUTORIAL/LAB\nQuestion : " + (i + 1).ToString() + "\n\n", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
                section_header.Alignment = Element.ALIGN_CENTER;
                doc.Add(section_header);

                string[] ques_tut_lab_close = new string[limit_tut_lab_Close];
                ques_tut_lab_close[0] = "How helpful was the tutorial/lab: \n5(Very helpful) - 1(Not at all)";

                Paragraph ques_string = new Paragraph(ques_tut_lab_close[i], FontFactory.GetFont(FontFactory.TIMES_BOLD, 12));
                ques_string.Alignment = Element.ALIGN_CENTER;

                doc.Add(new Paragraph(ques_string));
                //doc.Add(new Paragraph("\n"));

                System.Drawing.Image testPie = System.Drawing.Image.FromFile(physicalApplicationPath + "\\App_Images\\pie.jpg");
                System.Drawing.Graphics graphicsPie = System.Drawing.Graphics.FromImage(testPie);
                //graphicsPie.DrawString("Title", fontTitle, new SolidBrush(Color.Black), new System.Drawing.Rectangle(0, 0, 460, titleHeight), stringFormat);

                int[] startPie = new int[6];
                int[] anglePie = new int[6];
                double[] angleActual = new double[6];
                double[] percActual = new double[6];

                startPie[0] = 0;
                anglePie[0] = 0;
                angleActual[0] = 0;
                percActual[0] = 0;

                for (int j = 1; j < 6; j++)
                {
                    startPie[j] = startPie[j - 1] + anglePie[j - 1];
                    angleActual[j] = (((double)pie_tut_lab_Close[i, j]) * 360) / totalResp;
                    if ((angleActual[j] < 1) && (angleActual[j] > 0))
                        anglePie[j] = 1;
                    else
                        anglePie[j] = (pie_tut_lab_Close[i, j] * 360) / totalResp;
                    percActual[j] = (pie_tut_lab_Close[i, j] / totalResp) * 100;

                }

                anglePie[5] = 360 - startPie[5];

                graphicsPie.FillPie(brushRed, rectanglePie, startPie[1], anglePie[1]);
                graphicsPie.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                graphicsPie.FillPie(brushBlue, rectanglePie, startPie[2], anglePie[2]);
                graphicsPie.FillPie(brushGreen, rectanglePie, startPie[3], anglePie[3]);
                graphicsPie.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                graphicsPie.FillPie(brushYellow, rectanglePie, startPie[4], anglePie[4]);
                graphicsPie.FillPie(brushViolet, rectanglePie, startPie[5], anglePie[5]);

                string imgPath = physicalApplicationPath + "\\App_Images\\Temp\\" + "pieChart_InstClose" + i.ToString() + ".png";
                graphicsPie.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                testPie.Save(imgPath, System.Drawing.Imaging.ImageFormat.Png);
                testPie.Dispose();
                graphicsPie.Dispose();
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imgPath);
                img.ScalePercent(60);
                img.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
                doc.Add(img);

                //Showing keys
                PdfPTable keyTable = new PdfPTable(3);
                PdfPCell[] cellRow1 = new PdfPCell[3];
                PdfPCell[] cellRow2 = new PdfPCell[3];
                PdfPCell[] cellRow3 = new PdfPCell[3];
                PdfPCell[] cellRow4 = new PdfPCell[3];
                PdfPCell[] cellRow5 = new PdfPCell[3];


                PdfPRow[] row_complete = new PdfPRow[5];

                Color[] keyColor = new Color[5];
                keyColor[0] = Color.Red;
                keyColor[1] = Color.Blue;
                keyColor[2] = Color.Green;
                keyColor[3] = Color.Yellow;
                keyColor[4] = Color.Violet;

                for (int keyi = 0; keyi < 5; keyi++)
                {
                    /*string keyImgPath =this.Request.PhysicalApplicationPath + "\\App_Images\\" + (keyi + 1).ToString() + ".png";

                    iTextSharp.text.Image keyImg = iTextSharp.text.Image.GetInstance(keyImgPath);
                    keyImg.ScalePercent(70);
                    keyImg.Alignment = iTextSharp.text.Image.TEXTWRAP | iTextSharp.text.Image.ALIGN_LEFT;*/
                    if (keyi == 0)
                    {
                        cellRow1[0] = new PdfPCell();
                        cellRow1[1] = new PdfPCell();
                        cellRow1[2] = new PdfPCell();

                        cellRow1[0].AddElement(new Paragraph("\n"));
                        cellRow1[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow1[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow1[2].AddElement(new Paragraph(pie_tut_lab_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow1);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 1)
                    {
                        cellRow2[0] = new PdfPCell();
                        cellRow2[1] = new PdfPCell();
                        cellRow2[2] = new PdfPCell();

                        cellRow2[0].AddElement(new Paragraph("\n"));
                        cellRow2[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow2[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow2[2].AddElement(new Paragraph(pie_tut_lab_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow2);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 2)
                    {
                        cellRow3[0] = new PdfPCell();
                        cellRow3[1] = new PdfPCell();
                        cellRow3[2] = new PdfPCell();

                        cellRow3[0].AddElement(new Paragraph("\n"));
                        cellRow3[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow3[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow3[2].AddElement(new Paragraph(pie_tut_lab_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow3);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 3)
                    {
                        cellRow4[0] = new PdfPCell();
                        cellRow4[1] = new PdfPCell();
                        cellRow4[2] = new PdfPCell();

                        cellRow4[0].AddElement(new Paragraph("\n"));
                        cellRow4[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow4[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow4[2].AddElement(new Paragraph(pie_tut_lab_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow4);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                    if (keyi == 4)
                    {
                        cellRow5[0] = new PdfPCell();
                        cellRow5[1] = new PdfPCell();
                        cellRow5[2] = new PdfPCell();

                        cellRow5[0].AddElement(new Paragraph("\n"));
                        cellRow5[0].BackgroundColor = new BaseColor(keyColor[keyi]);
                        cellRow5[1].AddElement(new Paragraph((keyi + 1).ToString()));
                        cellRow5[2].AddElement(new Paragraph(pie_tut_lab_Close[i, keyi + 1].ToString() + " / " + totalResp.ToString()));

                        row_complete[keyi] = new PdfPRow(cellRow5);
                        keyTable.Rows.Add(row_complete[keyi]);
                    }
                }

                doc.Add(keyTable);
            }


        }

        else
        {
            doc.Add(new Paragraph("\n\nNo Responses Received"));
        }


        //CLOSING THE PDF

        doc.Close();

    }


}

